var prefix = jsctx + "/tenant/user";

var onlineUser = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listOnlineUser",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            loginName: $('#loginName').val()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'userId', title: '#', visible: false},
                        {field: 'sessionId', title: '会话ID'},
                        {field: 'loginName', title: '名称'},
                        {field: 'realName', title: '姓名'},
                        {field: 'roleName', title: '角色'},
                        {field: 'host', title: '主机'},
                        {field: 'startTimestamp', title: '创建时间'},
                        {field: 'lastAccessTime', title: '最后访问时间'},
                        {field: 'mobile', title: '手机'},
                        {
                            field: 'status',
                            title: '状态',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-primary">正常</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-danger">冻结</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {
                                var d = '<a class="btn btn-warning btn-sm ' + has_delete + '" href="#" title="删除" onclick="onlineUser.remove(\''
                                    + row.sessionId + '\',' + '\'' + row.loginName
                                    + '\')"><i class="fa fa-remove">删除</i></a> ';

                                return d;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    // function refresh() {
    //     layer.confirm('确定刷新授权？', {
    //         btn: ['确定', '取消']
    //     }, function (index) {
    //         $.ajax({
    //             url: prefix + "/refresh",
    //             type: "post",
    //             success: function (data) {
    //                 layer.close(index);
    //                 if (data.status == 200) {
    //                     layer.msg("刷新成功");
    //                     reLoad();
    //                 } else {
    //                     layer.msg(data.message);
    //                 }
    //             }
    //         });
    //     })
    // }

    function remove(sessionId, name) {
        layer.confirm('确定要删除选中的记录？' + name, {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/forceLogout",
                type: "post",
                data: {
                    'sessionId': sessionId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        remove: remove
    };
})();

